﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml.Serialization;
using Report.Entity;
using Report.Properties;

namespace Report.Service
{
    public class GomezService
    {
        private readonly HttpService httpService=new HttpService();

        public bool Login()
        {
            Dictionary<string, string> parameter = new Dictionary<string, string>
                                                       {
                                                           {"action", "login"},
                                                           {"form_string", ""},
                                                           {"pwd", Settings.Default.PassWord},
                                                           {"query_string", ""},
                                                           {"server_string", ""},
                                                           {"url_string", ""},
                                                           {"username", Settings.Default.User}
                                                       };

            HttpWebRequest webRequest = httpService.ConfigWebRequest(Settings.Default.LoginUrl, parameter);

            string responsetext = httpService.ExecuteRequestAsString(webRequest);

            return true;
        }

        public GomezTestCollection LoadTest()
        {
            HttpWebRequest webRequest = httpService.ConfigWebRequest(Settings.Default.LoadTestUrl + "?AccountID=10763&Active=inactive&agentType=-1&bmSub=1");

            string responsetext = httpService.ExecuteRequestAsString(webRequest);

            XmlSerializer xmlSerializer=new XmlSerializer(typeof(GomezTestCollection));

            GomezTestCollection gomezTestCollection = (GomezTestCollection)xmlSerializer.Deserialize(new StringReader(responsetext));

            return gomezTestCollection;
        }

        public bool CreateChart(DateTime startDate, DateTime endDate)
        {
            Dictionary<string, string> parameter = new Dictionary<string, string>
                                                       {
                                                           {"datagroups", ""},
                                                           {"days", "all"},
                                                           {"endDate", endDate.ToString("yyyy/MM/dd")},
                                                           {"endFilterDay", "1"},
                                                           {"endTime", endDate.ToString("hh:mm")},
                                                           {"period", "userdef"},
                                                           {"selectedMBGMids", "MBGMonitor:632839:false: 201105_webtest1"}, //?
                                                           {"startDate", startDate.ToString("yyyy/MM/dd")},
                                                           {"startFilterDay", "1"},
                                                           {"startFilterTime", "00:00"},
                                                           {"startTime", startDate.ToString("hh:mm")},
                                                           {"timeframe", "1"},
                                                           {"x", "51"}, //?
                                                           {"y", "0"}  //?
                                                       };
            HttpWebRequest webRequest = httpService.ConfigWebRequest(Settings.Default.CreateChartUrl,parameter);

            return true;
        }
    }
}
